package com.lemon.funplus;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

public class CommentActivity extends Activity {
	public static String FB_CM = "url_fb";
	private WebView webViewFB = null;
	private WebView childWebView = null;
	private LinearLayout parentLayout;
	String fbcomment = "";
	ProgressBar progressBar;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.comment_layout);
		getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
		this.overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
		Bundle extras = getIntent().getExtras();
		if (getIntent().hasExtra(FB_CM))
			fbcomment = extras.getString(FB_CM);
		progressBar = (ProgressBar) findViewById(R.id.progressBar_loadcomment);
		webViewFB = new WebView(this);
		webViewFB.setWebViewClient(new FaceBookClient());
		webViewFB.setWebChromeClient(new FBChromeClient());
		webViewFB.setWebViewClient(new WebViewClient() {
			public void onPageFinished(WebView view, String url) {
				progressBar.setVisibility(View.GONE);
			}
		});
		parentLayout = (LinearLayout) findViewById(R.id.parentLayout);

		webViewFB.setLayoutParams(getLayoutParams());

		final WebSettings webSettings = webViewFB.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
		webSettings.setSupportMultipleWindows(true);
		parentLayout.addView(webViewFB);
		webViewFB.loadDataWithBaseURL("http://funplus.vn/", getHTMLData(fbcomment), "text/html", null, "");

	}

	private String getHTMLData(String newsURL) {

		StringBuffer sb = new StringBuffer();
		sb.append("<html>");
		sb.append("<head>");
		// sb.append("<title>Anggota TNI ikut demo untuk beri rasa aman</title>");
		sb.append("<title>Funplus.vn</title>");
		sb.append("</head>");
		sb.append("<body>");
		// sb.append("<h1>Anggota TNI ikut demo untuk beri rasa aman</h1>");
//		sb.append("<h1>Comment facebook</h1>");
//		sb.append("<div style='color: #959595;'>Saucaca, 25-02-2015</div>");
		sb.append("<div class=\"fb-comments\" data-href=\"" + newsURL + "\" data-num-posts=\"5\" data-width=\"680\"></div>");
		sb.append("<div id=\"fb-root\"></div>");
		sb.append("<script>(function(d, s, id) {");
		sb.append("var js, fjs = d.getElementsByTagName(s)[0];");
		sb.append("if (d.getElementById(id)) {return;}");
		sb.append("js = d.createElement(s); js.id = id;");
		sb.append("js.src = \"//connect.facebook.net/en_US/all.js#xfbml=1&appId=1375226042786650\";");
		sb.append("fjs.parentNode.insertBefore(js, fjs);");
		sb.append("}(document, 'script', 'facebook-jssdk'));");
		sb.append("</script>");
		sb.append("</body>");
		sb.append("</html>");

		return sb.toString();
	}

	private LinearLayout.LayoutParams getLayoutParams() {
		return new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
	}

	private class FaceBookClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			Log.i("REQUEST URL", url);
			return false;
		}
	}

	final class FBChromeClient extends WebChromeClient {

		@Override
		public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
			childWebView = new WebView(CommentActivity.this);
			childWebView.getSettings().setJavaScriptEnabled(true);
			childWebView.getSettings().setSupportZoom(true);
			childWebView.getSettings().setBuiltInZoomControls(true);
			childWebView.setWebViewClient(new FaceBookClient());
			childWebView.setWebChromeClient(this);
			childWebView.setLayoutParams(CommentActivity.this.getLayoutParams());

			parentLayout.addView(childWebView);

			childWebView.requestFocus();
			webViewFB.setVisibility(View.GONE);
			WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
			transport.setWebView(childWebView);
			resultMsg.sendToTarget();
			return true;
		}

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			CommentActivity.this.setProgress(newProgress * 100);
		}

		@Override
		public void onCloseWindow(WebView window) {
			parentLayout.removeViewAt(parentLayout.getChildCount() - 1);
			childWebView = null;
			webViewFB.setVisibility(View.VISIBLE);
			progressBar.setVisibility(View.GONE);
			webViewFB.requestFocus();
		}

	}

}
